package com.zhihui.online.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhihui.online.dto.admin.AdminInfoQueryDTO;
import com.zhihui.online.entity.AdminInfo;
import com.zhihui.online.exception.MyCustomerException;
import com.zhihui.online.mapper.AdminInfoMapper;
import com.zhihui.online.service.AdminInfoService;
import com.zhihui.online.utils.ResultCodeEnum;
import com.zhihui.online.vo.AdminInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author 程序员阿良,21岁纯情后爸,懒得想名字
 * @since 2024-08-01
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo> implements AdminInfoService {

    /**
     * 修改账户状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        //查询数据库，判断使用有数据
        AdminInfo adminInfo = this.baseMapper.selectById(id);
        if (adminInfo == null) {
            throw new MyCustomerException(ResultCodeEnum.ACCOUNT_ERROR);
        }
        //封装存储对象
        AdminInfo adminInfo1 = new AdminInfo();
        adminInfo1.setStatus(status);
        adminInfo1.setId(id);
        //更新对象
        this.baseMapper.updateById(adminInfo1);
    }

    /**
     * 获得脱敏后的视图对象
     * @param adminInfo
     * @return
     */
    @Override
    public AdminInfoVO getVO(AdminInfo adminInfo) {
        // 判断对象是否为空
        if (adminInfo == null) {
            throw  new MyCustomerException(ResultCodeEnum.USER_NOT_EXIST);
        }
        //拷贝对象
        AdminInfoVO adminInfoVO = new AdminInfoVO();
        BeanUtils.copyProperties(adminInfo, adminInfoVO);
        return adminInfoVO;
    }

    @Override
    public IPage<AdminInfoVO> selectPage(IPage<AdminInfoVO> iPage, AdminInfoQueryDTO adminInfoQueryDTO) {

        IPage<AdminInfoVO> adminInfoVOIPage = this.baseMapper.selectPage(iPage,adminInfoQueryDTO);
        return adminInfoVOIPage != null ? adminInfoVOIPage : new Page<>();
    }
}
